package com.cms.controller.common;

import com.cms.utils.PathUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller("commonFileController")
@RequestMapping("/common/file")
public class FileController {

	@RequestMapping("/download")
	public void download(String fileKey,HttpServletRequest request, HttpServletResponse response) throws Exception{
		// 读文件
		File file = new File(PathUtils.getWebRootPath()+fileKey);
		if (!file.exists()) {
			// 如果不存在则抛出异常，由全局异常处理类去处理。
			throw new HttpMediaTypeNotAcceptableException("文件不存在");
		}
		// 获取用户名
		String fileName = file.getName();
		// 重置response
		response.reset();
		// ContentType，即告诉客户端所发送的数据属于什么类型
		response.setContentType("application/octet-stream; charset=UTF-8");
		// 获得文件的长度
		response.setHeader("Content-Length", String.valueOf(file.length()));
		// 设置编码格式
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
		// 发送给客户端的数据
		OutputStream outputStream = response.getOutputStream();
		byte[] buff = new byte[1024];
		BufferedInputStream bis = null;
		// 读取文件
		bis = new BufferedInputStream(new FileInputStream(new File(PathUtils.getWebRootPath()+fileKey)));
		int i = bis.read(buff);
		// 只要能读到，则一直读取
		while (i != -1) {
			// 将文件写出
			outputStream.write(buff, 0, buff.length);
			// 刷出
			outputStream.flush();
			i = bis.read(buff);
		}
	}
}
